package Core.LoginServer;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

import javax.xml.bind.annotation.adapters.HexBinaryAdapter;

class ServerThread extends Thread {
	private final Socket socket;
	private InputStream in;
	private OutputStream out;
	
	public ServerThread(Socket socket) throws IOException {
		this.socket = socket;
		 in = socket.getInputStream();
		 out =  new BufferedOutputStream(socket.getOutputStream());
		start();
	}

	public void run() {
		try {
			byte[] header = new byte[10];
			byte[] size = new byte[2];
			byte[] msg;
			int length = 0 ;
			while(true) {
				in.read(header, 0, header.length);
				length= size[0]*256 + size[1];  
				msg = new byte[length];
				in.read(msg,0, length);
				length = header.length + size.length +msg.length;
				byte[] stream = new byte[length];
				System.arraycopy(header, 0, stream, 0, header.length);
				System.arraycopy(size, 0, stream, header.length, size.length);
				System.arraycopy(msg, 0, stream, header.length+size.length, msg.length);
				System.out.print(new String(stream) +"  "+ stream.length);
			}

		} catch (Throwable t) {
			System.out.println("Caught " + t + " - closing thread");
		}
	}
}